<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="role_name">
        <el-input v-model="form.role_name" />
      </el-form-item>
      <el-form-item label="菜单">
        <div class="vab-tree-border">
          <el-tree
            ref="menutree"
            :data="list"
            :default-checked-keys="form.menu_ids_array"
            :default-expand-all="true"
            :default-expanded-keys="[]"
            node-key="id"
            show-checkbox
          >
            <template #default="{ data }">
              <span>{{ data.title }}</span>
            </template>
          </el-tree>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    defineComponent,
    getCurrentInstance,
    onMounted,
    reactive,
    toRefs,
    nextTick,
  } from 'vue'
  import { doEdit } from '@/api/roleManagement'
  import { getList } from '@/api/router'

  export default defineComponent({
    name: 'RoleManagementEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()

      const state = reactive({
        formRef: null,
        menutree: null,
        form: {},
        rules: {
          role_name: [
            { required: true, trigger: 'blur', message: '请输入角色名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        list: [],
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
          state.form = {}
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
        nextTick(() => {
          state['menutree'].setCheckedKeys(
            state.form.menu_ids_array ? state.form.menu_ids_array : []
          )
        })
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const fetchData = async () => {
        const {
          data: { list },
        } = await getList({ type: 'menu' })
        state.list = list
      }
      const save = () => {
        state.form.menu_ids = state['menutree'].getCheckedKeys()
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      onMounted(() => {
        fetchData()
      })
      return {
        ...toRefs(state),
        showEdit,
        close,
        fetchData,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    padding: $base-padding;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
